//
//  RouteLineViewController.m
//  Marketing
//
//  Created by HanenDev on 16/3/8.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "RouteLineViewController.h"
#import "RouteCell1.h"
#import "RouteCell2.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#define  ROUND 8.0f
#define STARTX [UIView getWidth:20]
#define WALKING @"walking"
#define BUS @"bus"
#define CAR @"car"
#define STATION @"station"
#define METRO @"metro"
@interface RouteLineViewController ()
{
    UIView *_topView;
     UITableView * _tableView;
    NSMutableArray  *_busLineArray;
}
@end

@implementation RouteLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = graySectionColor;
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"路线详情"];
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    _busLineArray = [NSMutableArray array];
    [self getData];
    [self topview];
    [self createView];
    
    
    
}
- (void)getData
{
    if ([self.typeFlag isEqualToString:@"步行"]) {
//        NSLog(@"步行数据%@",self.routeArray);
        for (int i = 0; i< self.routeArray.count; i++) {
            BMKTransitStep* transitStep = _routeArray[i];
//
             NSLog(@"步行数据%@",transitStep.instruction);
        }
    }else if ([self.typeFlag isEqualToString:@"公交"]) {
        for (int i = 0; i< self.routeArray.count; i++) {
            BMKTransitStep* transitStep = _routeArray[i];
            
            NSLog(@"步行数据按顺序==  %@",transitStep.instruction);
            NSString *lineStr = transitStep.instruction;
            if (isIos8Orlater) {
                if([lineStr containsString:@","]){
                
                    NSArray *compontArr = [lineStr componentsSeparatedByString:@","];
                    for (NSString *subStr  in compontArr) {
                        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                            if ([subStr containsString:@"步行"]) {
                                [dict setObject:subStr forKey:WALKING];
                                [_busLineArray addObject:dict];
                                //                        return;
                            }else if([subStr containsString:@"到达"] && ![subStr containsString:@"到达终点站"]){
                                NSString *str = [subStr substringFromIndex:2];
                                [dict setObject:str forKey:STATION];
                                [_busLineArray addObject:dict];
                                //                        return;
                            }else if([subStr containsString:@"乘坐"] || [subStr containsString:@"换乘"]){
                                if ([subStr containsString:@"地铁"]) {
                                    [dict setObject:subStr forKey:METRO];
                                }else{
                                    [dict setObject:subStr forKey:BUS];
                                }
                                [_busLineArray addObject:dict];
                                //                        return;
                            }else if([subStr containsString:@"到达终点站"]){
                                return;
                            }
                        }
                }
            }else{
                NSRange ran = [lineStr rangeOfString:@","];
                if (ran.length > 0 ) {
                    
                    NSArray *compontArr = [lineStr componentsSeparatedByString:@","];
                    for (NSString *subStr  in compontArr) {
                        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                        if (isIos8Orlater) {
                            if ([subStr containsString:@"步行"]) {
                                [dict setObject:subStr forKey:WALKING];
                                [_busLineArray addObject:dict];
                                //                        return;
                            }else if([subStr containsString:@"到达"] && ![subStr containsString:@"到达终点站"]){
                                NSString *str = [subStr substringFromIndex:2];
                                [dict setObject:str forKey:STATION];
                                [_busLineArray addObject:dict];
                                //                        return;
                            }else if([subStr containsString:@"乘坐"] || [subStr containsString:@"换乘"]){
                                if ([subStr containsString:@"地铁"]) {
                                    [dict setObject:subStr forKey:METRO];
                                }else{
                                    [dict setObject:subStr forKey:BUS];
                                }
                                [_busLineArray addObject:dict];
                                //                        return;
                            }else if([subStr containsString:@"到达终点站"]){
                                return;
                            }
                        }else{
                            NSRange range1 = [subStr rangeOfString:@"步行"];
                            NSRange range2 = [subStr rangeOfString:@"到达"];
                            NSRange range3 = [subStr rangeOfString:@"到达终点站"];
                            NSRange range4 = [subStr rangeOfString:@"乘坐"];
                            NSRange range5 = [subStr rangeOfString:@"换乘"];
                            NSRange range6 = [subStr rangeOfString:@"地铁"];
                            //                            NSRange range7 = [subStr rangeOfString:@"乘坐"];
                            if (range1.length > 0) {
                                [dict setObject:subStr forKey:WALKING];
                                [_busLineArray addObject:dict];
                            }else if (range2.length >0 && range3.length == 0){
                                NSString *str = [subStr substringFromIndex:2];
                                [dict setObject:str forKey:STATION];
                                [_busLineArray addObject:dict];
                                
                            }else if (range4.length > 0 || range5.length >0){
                                if (range6.length > 0) {
                                    [dict setObject:subStr forKey:METRO];
                                }else if(range6.length == 0){
                                    [dict setObject:subStr forKey:BUS];
                                }
                                [_busLineArray addObject:dict];
                            }else if(range3.length >0){
                                return;
                            }
                        }
                        
                        
                    }

                }
            }
            
        }
//            if([lineStr containsString:@","]){
//                
//                NSArray *compontArr = [lineStr componentsSeparatedByString:@","];
//                for (NSString *subStr  in compontArr) {
//                    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//                    if (isIos8Orlater) {
//                        if ([subStr containsString:@"步行"]) {
//                            [dict setObject:subStr forKey:WALKING];
//                            [_busLineArray addObject:dict];
//                            //                        return;
//                        }else if([subStr containsString:@"到达"] && ![subStr containsString:@"到达终点站"]){
//                            NSString *str = [subStr substringFromIndex:2];
//                            [dict setObject:str forKey:STATION];
//                            [_busLineArray addObject:dict];
//                            //                        return;
//                        }else if([subStr containsString:@"乘坐"] || [subStr containsString:@"换乘"]){
//                            if ([subStr containsString:@"地铁"]) {
//                                [dict setObject:subStr forKey:METRO];
//                            }else{
//                                [dict setObject:subStr forKey:BUS];
//                            }
//                            [_busLineArray addObject:dict];
//                            //                        return;
//                        }else if([subStr containsString:@"到达终点站"]){
//                            return;
//                        }
//                    }else{
//                        NSRange range1 = [subStr rangeOfString:@"步行"];
//                        NSRange range2 = [subStr rangeOfString:@"到达"];
//                        NSRange range3 = [subStr rangeOfString:@"到达终点站"];
//                        NSRange range4 = [subStr rangeOfString:@"乘坐"];
//                        NSRange range5 = [subStr rangeOfString:@"换乘"];
//                        NSRange range6 = [subStr rangeOfString:@"地铁"];
////                            NSRange range7 = [subStr rangeOfString:@"乘坐"];
//                        if (range1.length > 0) {
//                            [dict setObject:subStr forKey:WALKING];
//                            [_busLineArray addObject:dict];
//                        }else if (range2.length >0 && range3.length == 0){
//                            NSString *str = [subStr substringFromIndex:2];
//                            [dict setObject:str forKey:STATION];
//                            [_busLineArray addObject:dict];
//
//                        }else if (range4.length > 0 || range5.length >0){
//                            if (range6.length > 0) {
//                                [dict setObject:subStr forKey:METRO];
//                            }else if(range6.length == 0){
//                                [dict setObject:subStr forKey:BUS];
//                            }
//                            [_busLineArray addObject:dict];
//                        }else if(range3.length >0){
//                               return;
//                        }
//                    }
//                    
//                    
//                }
//            }
        
           
//            BMKRouteNode *routNode = transitStep.entrace;
//            NSLog(@"路线入口====%@",routNode.title);
//            BMKRouteNode *exitNode = transitStep.exit;
//            NSLog(@"路线出口====%@",exitNode.title);
        
         NSLog(@"对应的显示数据%d",_busLineArray.count);
        for (NSMutableDictionary *dict  in _busLineArray) {
            NSLog(@"对应的显示数据%@",dict);
        }
        
    }else if ([self.typeFlag isEqualToString:@"驾车"]) {
        for (int i = 0; i< self.routeArray.count; i++) {
//            BMKTransitStep* transitStep = _routeArray[i];
//            
//            NSLog(@"步行数据%@",transitStep.instruction);
        }
    };
}
- (void)topview
{
    _topView = [ViewTool getLineViewWith:CGRectMake(0, 0, KSCreenW, 70.0f) withBackgroudColor:[UIColor whiteColor]];
    [self.view addSubview:_topView];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(STARTX, 15.0f, KSCreenW - 2*STARTX - 60, 20)];
    self.timeLabel.textColor = blackFontColor;
    [_topView addSubview:self.titleLabel];
    
    
    
    UIImageView *timeImage = [[UIImageView alloc]initWithFrame:CGRectMake(STARTX, self.titleLabel.maxY + 7, 15, 15)];
    timeImage.image = [UIImage imageNamed:@"HY_map_icon_time"];
    [_topView addSubview:timeImage];
    
    //时间
    self.subTitLab = [[UILabel alloc]initWithFrame:CGRectMake(timeImage.maxX + 5,self.titleLabel.maxY + 5, 80 , 20)];
    self.subTitLab.font = [UIFont systemFontOfSize:15.0f];
    self.subTitLab.textColor = blueFontColor;
    self.subTitLab.text = self.timeStr;
    [_topView addSubview:self.subTitLab];
    
    
    self.walkImage=[[UIImageView alloc]initWithFrame:CGRectMake(self.subTitLab.maxX + STARTX, timeImage.y, 20, 20)];
    self.walkImage.image = [UIImage imageNamed:@"HY_map_icon_walk"];
    [_topView addSubview:self.walkImage];
    
    
    self.walkLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.walkImage.maxX + 5, self.subTitLab.y, 150, 20)];
    self.walkLabel.font = [UIFont systemFontOfSize:15.0f];
    self.walkLabel.textColor = grayFontColor;
    self.walkLabel.text = self.distantsStr;
    [_topView addSubview:self.walkLabel];
 
    
    UIView *lineView =[[UIView alloc]initWithFrame:CGRectMake(0, _topView.height - 1, KSCreenW , 1)];
    lineView.backgroundColor = grayLineColor;
    [_topView addSubview:lineView];
    
    if ([self.typeFlag isEqualToString:@"步行"]) {
        self.titleLabel.text = @"步行方案";
    }else if([self.typeFlag isEqualToString:@"公交"]){
        NSString *vehicleName = @"";
        
        int num = 0;
        int num1 = 0;
        for (int i = 0; i < self.routeArray.count; i++) {
            BMKTransitStep* transitStep = [self.routeArray objectAtIndex:i];
            
            BMKVehicleInfo *infos = transitStep.vehicleInfo;
            //NSLog(@"%@ %@",transitStep.instruction,infos.title);
            //            NSLog(@"%@     %@    %@    %@    ",
            //                  transitStep.entrace.title,
            //                  transitStep.exit.title,
            //                  transitStep.instruction,
            //                  (transitStep.stepType == BMK_BUSLINE ? @"公交路段" : (transitStep.stepType == BMK_SUBWAY ? @"地铁路段" : @"步行路段"))
            //                  );
            if (transitStep.stepType==BMK_WAKLING)
            {
                NSLog(@"%@++++++++++++",transitStep.instruction);
                NSString *str = [transitStep.instruction componentsSeparatedByString:@"米"][0];
                NSString *string;
                if (isIos8Orlater) {
                    if([str containsString:@"步行"]){
                        string = [str componentsSeparatedByString:@"步行"][1];
                    }
                }else{
                    NSRange range = [str rangeOfString:@"步行"];
                    if (range.length > 0) {
                         string = [str componentsSeparatedByString:@"步行"][1];
                    }
                }
               
                int a = [string intValue];
                NSLog(@"%@=========步行数",string);
                num = num +a;
            }
            if (transitStep.stepType==BMK_BUSLINE | transitStep.stepType==BMK_SUBWAY) {
                NSString *str = [transitStep.instruction componentsSeparatedByString:@"经过"][1];
                NSString *string = [str componentsSeparatedByString:@"站,"][0];
                NSLog(@"%@//////////总站数",string);
                int a = [string intValue];
                
                num1 = num1 + a;
            }
            //判断是否有线路名称
            if (infos) {
                if ([vehicleName isEqualToString:@""]) {
                    vehicleName = [NSString stringWithFormat:@"%@",infos.title];
                }
                else{
                    vehicleName = [NSString stringWithFormat:@"%@ - %@",vehicleName,infos.title];
                }
            }
        }
        self.titleLabel.text = vehicleName;
        self.walkLabel.text = [NSString stringWithFormat:@"%d米",num];
    }else{
        self.titleLabel.text = @"驾车方案";
        self.timeLabel.text = self.timeStr;
        self.walkLabel.text = self.distantsStr;
    }
    
    
}
- (void)createView{
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, _topView.maxY + 15.0f, KSCreenW, KSCreenH-_topView.maxY - 64) style:UITableViewStylePlain];
    _tableView.backgroundColor = graySectionColor;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, 50)];
    headerView.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(23.0f - 10, 16, 20, 20 )];
    imageView.image=[UIImage imageNamed:@"HY_map_icon_begin"];
    //imageView.backgroundColor= [UIColor redColor];
    [headerView addSubview:imageView];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(imageView.maxX + 7, 5, KSCreenW-imageView.maxX, 40)];
    label.text=[NSString stringWithFormat:@"起点 (%@)",self.startStr];
     label.font = [UIFont systemFontOfSize:15.0f];
    [headerView addSubview:label];
    _tableView.tableHeaderView=headerView;
    
    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, 50)];
    footerView.backgroundColor = [UIColor whiteColor];
    UIImageView *picView=[[UIImageView alloc]initWithFrame:CGRectMake(23 - 10, 16, 25, 25)];
    picView.image=[UIImage imageNamed:@"HY_map_icon_end.png"];
//    picView.backgroundColor= greenBackColor;
    [footerView addSubview:picView];
    
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(picView.maxX + 7 , 5, KSCreenW-imageView.maxX, 40)];
    lab.font = [UIFont systemFontOfSize:15.0f];
    lab.text=[NSString stringWithFormat:@"终点 (%@)",self.endStr];
    [footerView addSubview:lab];
    
//    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(KSCreenW - STARTX-20, 21, 14, 8)];
//    [button setImage:[UIImage imageNamed:@"下拉收回"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(clickToHide:) forControlEvents:UIControlEventTouchUpInside];
//    [footerView addSubview:button];
    
    
    _tableView.tableFooterView=footerView;
}
- (void)clickToHide:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(getToHide:)]) {
        [self.delegate getToHide:btn];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.typeFlag isEqualToString:@"公交"]) {
           return _busLineArray.count;
    }else{
        return self.routeArray.count;
    }
 
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *content=[self.routeArray[indexPath.row] instruction];
//    CGFloat height=[self getHeightByString:content withFont:[UIFont systemFontOfSize:15.0f]];
//    
//    return height+20.0f;
    if ([self.typeFlag isEqualToString:@"公交"]) {
        NSMutableDictionary *dict = _busLineArray[indexPath.row];
        NSArray *keys = [dict allKeys];
        NSString *key = keys.firstObject;
        NSLog(@"建值%@",key);
        if ([key isEqualToString:BUS
             ] || [key isEqualToString:WALKING
                   ] || [key isEqualToString:METRO]) {
            return [RouteCell1 cellHeight];
        }else if ([key isEqualToString:STATION]){
            return [RouteCell2 cellHeight];
        }else{
            return 0;
        }
      
    }else{
        return 60.0f;
       
    }
//    NSMutableDictionary *dict = _busLineArray[indexPath.row];
//    NSArray *keys = [dict allKeys];
//    NSString *key = keys.firstObject;
//    NSLog(@"建值%@",key);
//    if ([key isEqualToString:BUS
//         ] || [key isEqualToString:WALKING
//               ]) {
//        return [RouteCell1 cellHeight];
//    }else if ([key isEqualToString:STATION]){
//        return [RouteCell2 cellHeight];
//    }else{
//        return 0;
//    }
//    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.typeFlag isEqualToString:@"公交"]) {
        NSMutableDictionary *dict = _busLineArray[indexPath.row];
        NSArray *keys = [dict allKeys];
        NSString *key = keys.firstObject;
        NSLog(@"=====%@",dict[key]);
        if ([key isEqualToString:BUS
             ] || [key isEqualToString:WALKING
                   ] || [key isEqualToString:METRO]) {
            RouteCell1 * cell = [RouteCell1 cellWithTableView:tableView];
            cell.infoLabel.text = dict[key];
            if ([key isEqualToString:METRO]) {
                cell.typeImageView.image = [UIImage imageNamed:@"HY_map_icon_metro"];
            }else if ([key isEqualToString:BUS]){
                 cell.typeImageView.image = [UIImage imageNamed:@"HY_map_icon_bus"];
            }else if ([key isEqualToString:WALKING]){
                cell.typeImageView.image = [UIImage imageNamed:@"HY_map_icon_walk"];
            }
//            cell.backgroundColor = greenBackColor;
            return cell;
        }else if ([key isEqualToString:STATION]){
            RouteCell2 * cell = [RouteCell2 cellWithTableView:tableView];
            cell.stationInfo = dict[key];
            if(indexPath.row == 0){
                cell.isGetON = 1;
            }
            if(indexPath.row == _busLineArray.count - 1){
                cell.isGetON = 2;
            }
//            cell.backgroundColor = NavBackColor;
            return cell;
        }
     
    }else{
        
        RouteCell1 * cell = [RouteCell1 cellWithTableView:tableView];
        BMKTransitStep *transit = self.routeArray[indexPath.row];
        cell.infoLabel.text = transit.instruction;
        return cell;

//        static NSString *identifier=@"cell";
//        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//        if (cell==nil)
//        {
//            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//            UIView * view=[[UIView alloc]init];
//            view.tag=1000;
//            view.backgroundColor=[UIColor greenColor];
//            [cell addSubview:view];
//            
//            UIImageView *imgView = [[UIImageView alloc]init];
//            imgView.tag = 3000;
//            [cell addSubview:imgView];
//            
//            UILabel *label=[[UILabel alloc]init];
//            label.tag=2000;
//            [cell addSubview:label];
//        }
//        cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        
//        UILabel *label=[cell viewWithTag:2000];
//        NSString *content=[self.routeArray[indexPath.row] instruction];
//        label.text=content;
//        label.numberOfLines=0;
//        label.font=[UIFont systemFontOfSize:15.0f];
//        CGFloat height=[self getHeightByString:content withFont:[UIFont systemFontOfSize:15.0f]];
//        label.frame=CGRectMake(STARTX + 20+5, 10, KSCreenW-70, height);
//        
//        UIImageView *imageV = [cell viewWithTag:3000];
//        if (self.result == 1) {
//            if (indexPath.row == 0 || indexPath.row == self.routeArray.count - 1) {
//                imageV.frame = CGRectMake(STARTX, 0, 20 , label.height +20);
//                imageV.image = [UIImage imageNamed:@"点"];
//                //imageV.backgroundColor = [UIColor redColor];
//            }else{
//                imageV.frame = CGRectMake(STARTX +9, 0, 2, label.height + 20);
//                imageV.backgroundColor = blueFontColor;
//            }
//        }else{
//            imageV.frame = CGRectMake(STARTX +9, 0, 2, label.height + 20);
//            imageV.backgroundColor = blueFontColor;
//        }
//        if(self.result == 1){
//            BMKTransitStep* transitStep = [self.routeArray objectAtIndex:indexPath.row];
//            if (transitStep.stepType==BMK_WAKLING)
//            {
//                imageV.frame = CGRectMake(STARTX + 9, 0, 2, label.height +20);
//                imageV.image = [UIImage imageNamed:@"点"];
//                
//                //            UIImageView *ima;
//                //            imageV.transform=CGAffineTransformMakeRotation(M_PI);
//                
//            }else{
//                imageV.frame = CGRectMake(STARTX +9, 0, 2, label.height + 20);
//                imageV.backgroundColor = [UIColor colorWithRed:143/255 green:217/255 blue:223/255 alpha:0.6];
//            }
//        }
//        
//        
//        
//        UIView *view=[cell viewWithTag:1000];
//        view.frame=CGRectMake(label.x/2, label.centerY, ROUND, ROUND);
//        view.layer.cornerRadius=ROUND/2.0f;
//        view.layer.masksToBounds=YES;
//        
//        cell.textLabel.text=[[self.routeArray objectAtIndex:indexPath.row] instruction];
//        cell.textLabel.numberOfLines=0;
//        
//        return cell;
    }
 
    return nil;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
-(CGFloat)getHeightByString:(NSString*)string   withFont:(UIFont*)font
{
    CGSize size = [string boundingRectWithSize:CGSizeMake(KSCreenW-50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return size.height ;
    
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
